﻿@page "/login"
@using Shared
@using IMaoTai.Core.Domain
@using IMaoTai.Core
@using System
@using System.Web
@using Microsoft.AspNetCore.Components
@using AlertTypes = Masa.Blazor.AlertTypes
@using IMaoTai.MasaUI.Core
@layout EmptyLayout
@inject NavigationManager NavigationManager

<MRow Class="max-height ma-0">
    <MCol Md=4 Sm=12 Class="neutral-lighten-5">
        <image class="ml-12 mt-12" style="height:40px;"
               src="/img/login/2.jpg">
        </image>
        <div class="d-flex pb-12" style="height: calc(100% - 88px);">
            <div style="width:450px;" class="ma-auto">
                <h3 class="neutral-lighten-1--text">让i茅台预约更简单</h3>
                <image class="mt-12" src="/img/login/left.svg"></image>
            </div>
        </div>
    </MCol>
    <MCol Md=8 Sm=12 Class="fill-lighten-1 block-center">
        <MCard Width=510 Height=680 Class="mx-auto my-auto">
            <div style="height:60px;"></div>
            <div class="mt-2 mx-auto text-center">
                <MAvatar Size=80>
                    <MImage Src="/img/login/2.jpg">
                    </MImage>
                </MAvatar>
                <h5 class="mt-2 mb-12">欢迎来到i茅台预约! 👋</h5>
                <MAlert Type="@_alertTypes" Value="@_alertShow" Dismissible Width=340>
                    @Msg
                </MAlert>
                <MTextField TValue="string" Label="用户名" @bind-Value="@ApplicationUser.UserName" Placeholder="@ApplicationUser.UserName" Outlined HideDetails="@("auto")">
                </MTextField>
                <MTextField TValue="string" Class="mt-10" Label="密码"
                            @bind-Value="@ApplicationUser.Password"
                            Placeholder="@ApplicationUser.Password"
                            Type="@(_show ? "text" : "password")"
                            AppendIcon="@(_show ? "mdi-eye" : "mdi-eye-off")"
                            OnAppendClick="()=>_show = !_show" Outlined HideDetails="@("auto")">
                </MTextField>
                <MButton Class="mt-11 rounded-4 text-btn white--text" OnClick=Login Height=46 Width=340 Color="primary">
                    Login
                </MButton>
            </div>
        </MCard>
    </MCol>
</MRow>

@code{

    [Inject]
    HostAuthenticationStateProvider AuthStateProvider { get; set; }


    public async Task Login(MouseEventArgs args)
    {
        var returnUrl = HttpUtility.ParseQueryString(new Uri(NavigationManager.Uri).Query)["returnUrl"];
        if (returnUrl == null) returnUrl = "/";
        bool result = await AuthStateProvider.Login(ApplicationUser, returnUrl);
        _alertShow = true;
        if (!result)
        {
            _alertTypes = AlertTypes.Error;
            Msg = "账号或者密码错误";       
        }
        else
        {
            Msg = "登录成功";
            NavigationManager.NavigateTo(returnUrl, true, true);
        }
        return;
    }

    private string Msg;
    private AlertTypes _alertTypes = AlertTypes.Success;
    private bool _alertShow = false;



    private bool _show;

    [Parameter]
    public bool HideLogo { get; set; }

    [Parameter]
    public double Width { get; set; } = 410;


    public UserLogin ApplicationUser { get; set; } = new UserLogin()
        {
            UserName = "admin",
            Password = "123456",
        };

   public string? style;

    protected override void OnInitialized()
    {
        base.OnInitialized();
    }
}

